package com.rpframework.module.common.bottom.controller;

import com.rpframework.core.json.JsonResp;
import com.rpframework.core.json.ParameterException;
import com.rpframework.module.common.bottom.service.SmsService;
import com.rpframework.module.common.service.UserTokenService;
import com.rpframework.module.common.url.RequestDescription;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 短信
 * Created by jump on 2016/8/5.
 */
@RequestMapping
@RequestDescription("短信")
@ResponseBody
public class SmsController extends BaseController {

    private final SmsService smsService;

    @Autowired
    public SmsController(UserTokenService uerTokenService, SmsService smsService) {
        super(uerTokenService);
        this.smsService = smsService;
    }

    @RequestMapping
    @RequestDescription("发送验证码")
    public JsonResp sendByCode(String phone, String type) {
        return smsService.sendSmsByVerifyCodeJson(phone, type);
    }

    @RequestMapping
    @RequestDescription("检测验证码")
    public JsonResp checkCode(String phone, String type, String smsCode) {
        if (StringUtils.isBlank(type))
            throw new ParameterException("短信模板ID不能为空");
        if (StringUtils.isBlank(smsCode))
            throw new ParameterException("验证码不能为空");
        if (StringUtils.isBlank(phone))
            throw new ParameterException("手机号不能为空");

        if (!smsService.checkSmsVerifyCode(phone, smsCode, type))
            throw new ParameterException("验证码错误");
        return JsonResp.ok("验证通过");
    }
}
